package com.bnlr.lrparking.appui.mine.recentparkrecord;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bnlr.lrparking.R;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.appui.sharevalue.recharge.TopUpActivity;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * description: 贡献值支付结果页面
 * ContributionPayResult class
 *
 * @author : Licy
 * @date : 2018/12/29
 */
public class ContributionPayResult extends BaseActivity {

    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.tv_nav_right)
    TextView tvNavRight;
    @BindView(R.id.rela_nav_back)
    RelativeLayout relaNavBack;
    @BindView(R.id.img_pay_result)
    ImageView imgPayResult;
    @BindView(R.id.tv_pay_result)
    TextView tvPayResult;
    @BindView(R.id.tv_pay_result_tip)
    TextView tvPayResultTip;
    @BindView(R.id.btn_re_pay)
    Button btnRePay;

    /**
     * 支付结果
     * 0  支付成功
     * 1  支付失败，贡献值不足
     * 2  支付失败，网络异常
     */
    private int result = -1;
    private String shouYiValue;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contributionpayresult);
        ButterKnife.bind(this);
        mContext =this;
        result = getIntent().getIntExtra("result", -1);
        getPersonInfo();
        initView();
        initListener();
        dealResult();
    }

    public void initView() {
        tvNavTitle.setText("支付结果");
    }

    public void initListener() {

        relaNavBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tvNavRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnRePay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (result) {
                    // 支付失败，贡献值不足
                    case 1:
                        startActivity(new Intent(mContext, TopUpActivity.class).putExtra("shouyi",shouYiValue));
                        finish();
                        break;
                    // 支付失败，网络异常
                    case 2:
                        finish();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    public void dealResult() {

        switch (result) {
            // 支付成功
            case 0:
                imgPayResult.setImageResource(R.mipmap.pay_seccess);
                tvPayResult.setText("支付成功！");
                tvPayResult.setTextColor(getResources().getColor(R.color.main_color));
                tvPayResultTip.setText("车位停车费支付成功,\n可在个人中心——停车记录查看");
                btnRePay.setVisibility(View.GONE);
                break;
            // 支付失败，贡献值不足
            case 1:
                imgPayResult.setImageResource(R.mipmap.icon_pay_fail);
                tvPayResult.setText("支付失败！");
                tvPayResult.setTextColor(getResources().getColor(R.color.text_ea5544));
                tvPayResultTip.setText("提示：贡献值不足！");
                btnRePay.setVisibility(View.VISIBLE);
                btnRePay.setText("立即充值");
                break;
            // 支付失败，网络异常
            case 2:
                imgPayResult.setImageResource(R.mipmap.icon_pay_fail);
                tvPayResult.setText("支付失败！");
                tvPayResult.setTextColor(getResources().getColor(R.color.text_ea5544));
                tvPayResultTip.setText("提示：网络出了小差，请检查网络！");
                btnRePay.setVisibility(View.VISIBLE);
                btnRePay.setText("重新支付");
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 获取贡献值  收益值
     */
    private void getPersonInfo() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetUserInfoById");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            // 添加到请求队列
            CallServer.getRequestInstance().add(mContext, mRequest,
                    new CustomHttpListener<JSONObject>(mContext, false, null) {
                        @Override
                        public void doWork(JSONObject data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    JSONObject object = obj.getJSONObject("data");
                                    // 收益值
                                    shouYiValue = object.getString("incomevalue");
                                } else {

                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {

        }
    }
}
